package cn.edu.ujn.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import cn.edu.ujn.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import cn.edu.ujn.system.domain.FAdmin;
import cn.edu.ujn.system.mapper.FBuildingMapper;
import cn.edu.ujn.system.domain.FBuilding;
import cn.edu.ujn.system.service.IFBuildingService;
import cn.edu.ujn.common.core.text.Convert;

/**
 * 宿舍楼信息管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-22
 */
@Service
public class FBuildingServiceImpl implements IFBuildingService 
{
    @Autowired
    private FBuildingMapper fBuildingMapper;

    /**
     * 查询宿舍楼信息管理
     * 
     * @param buildingId 宿舍楼信息管理主键
     * @return 宿舍楼信息管理
     */
    @Override
    public FBuilding selectFBuildingByBuildingId(Long buildingId)
    {
        return fBuildingMapper.selectFBuildingByBuildingId(buildingId);
    }

    /**
     * 查询宿舍楼信息管理列表
     * 
     * @param fBuilding 宿舍楼信息管理
     * @return 宿舍楼信息管理
     */
    @Override
    public List<FBuilding> selectFBuildingList(FBuilding fBuilding)
    {
        return fBuildingMapper.selectFBuildingList(fBuilding);
    }

    /**
     * 新增宿舍楼信息管理
     * 
     * @param fBuilding 宿舍楼信息管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFBuilding(FBuilding fBuilding)
    {
        int rows = fBuildingMapper.insertFBuilding(fBuilding);
        insertFAdmin(fBuilding);
        return rows;
    }

    /**
     * 修改宿舍楼信息管理
     * 
     * @param fBuilding 宿舍楼信息管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFBuilding(FBuilding fBuilding)
    {
        fBuildingMapper.deleteFAdminByAdminId(fBuilding.getBuildingId());
        insertFAdmin(fBuilding);
        return fBuildingMapper.updateFBuilding(fBuilding);
    }

    /**
     * 批量删除宿舍楼信息管理
     * 
     * @param buildingIds 需要删除的宿舍楼信息管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFBuildingByBuildingIds(String buildingIds)
    {
        fBuildingMapper.deleteFAdminByAdminIds(Convert.toStrArray(buildingIds));
        return fBuildingMapper.deleteFBuildingByBuildingIds(Convert.toStrArray(buildingIds));
    }

    /**
     * 删除宿舍楼信息管理信息
     * 
     * @param buildingId 宿舍楼信息管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFBuildingByBuildingId(Long buildingId)
    {
        fBuildingMapper.deleteFAdminByAdminId(buildingId);
        return fBuildingMapper.deleteFBuildingByBuildingId(buildingId);
    }

    /**
     * 新增管理员信息管理信息
     * 
     * @param fBuilding 宿舍楼信息管理对象
     */
    public void insertFAdmin(FBuilding fBuilding)
    {
        List<FAdmin> fAdminList = fBuilding.getFAdminList();
        Long buildingId = fBuilding.getBuildingId();
        if (StringUtils.isNotNull(fAdminList))
        {
            List<FAdmin> list = new ArrayList<FAdmin>();
            for (FAdmin fAdmin : fAdminList)
            {
                fAdmin.setAdminId(buildingId);
                list.add(fAdmin);
            }
            if (list.size() > 0)
            {
                fBuildingMapper.batchFAdmin(list);
            }
        }
    }
}
